package com.example.config.mybatis;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Mybatis generator 自定义 Comment 注释生成器，通过继承 DefaultCommentGenerator 类
 *
 * @Author 刘仁奎
 */
public class MyCommentGenerator extends DefaultCommentGenerator {

    private String author = "刘仁奎";
    private String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

    /**
     * 类
     */
    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        topLevelClass.addJavaDocLine("/**");
        String remark = introspectedTable.getRemarks();
        if (StringUtils.isNotBlank(remark)) {
            topLevelClass.addJavaDocLine(" * " + remark);
        }
        topLevelClass.addJavaDocLine(" * ");
        topLevelClass.addJavaDocLine(" * " + "@author " + author);
        topLevelClass.addJavaDocLine(" * " + "@date " + date);
        topLevelClass.addJavaDocLine(" */");
    }

    /**
     * 字段
     */
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        String remark = introspectedColumn.getRemarks();
        if (StringUtils.isNotBlank(remark)) {
            field.addJavaDocLine("/**");
            field.addJavaDocLine(" * " + remark);
            field.addJavaDocLine(" */");
        }
    }

    /**
     * get方法
     */
    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
    }

    /**
     * setter方法
     */
    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
    }

    @Override
    public void addComment(XmlElement xmlElement) {
    }

    /**
     * 方法
     */
    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
        String remark = introspectedTable.getRemarks();
//        System.out.println(introspectedTable.getFullyQualifiedTable());

        if (StringUtils.isNotBlank(remark)) {
            if (method.getName().equalsIgnoreCase("insert")) {
                method.addJavaDocLine("/**");
                method.addJavaDocLine(" * " + "插入对象");
                method.addJavaDocLine(" */");
            } else if (method.getName().equalsIgnoreCase("insertSelective")) {
                method.addJavaDocLine("/**");
                method.addJavaDocLine(" * " + "选择性插入 - " + remark);
                method.addJavaDocLine(" */");
            } else if (method.getName().equalsIgnoreCase("selectByPrimaryKey")) {
                method.addJavaDocLine("/**");
                method.addJavaDocLine(" * " + "根据主键查询 - " + remark);
                method.addJavaDocLine(" */");
            } else if (method.getName().equalsIgnoreCase("updateByPrimaryKeySelective")) {
                method.addJavaDocLine("/**");
                method.addJavaDocLine(" * " + "选择性更新 - " + remark);
                method.addJavaDocLine(" */");
            } else if (method.getName().equalsIgnoreCase("updateByPrimaryKey")) {
                method.addJavaDocLine("/**");
                method.addJavaDocLine(" * " + "根据主键更新 - " + remark);
                method.addJavaDocLine(" */");
            } else if (method.getName().equalsIgnoreCase("deleteByPrimaryKey")) {
                method.addJavaDocLine("/**");
                method.addJavaDocLine(" * " + "根据主键删除 - " + remark);
                method.addJavaDocLine(" */");
            }
        }
    }
}
